import React, { Component } from 'react';
import { use_logo } from '../../utils/api';
import { withRouter } from 'react-router-dom';
import { Link } from 'react-router-dom';
class logo extends Component {
  state = {
    phone: '',
    pass: ''
  }
  handenlclick(type) {
    return (e) => {
      this.setState({
        [type]: e.target.value
      })
      console.log(this.state);
    }

    // this.setState({
    //   [e.currentTarget.name]: e.currentTarget.value
    // })
    console.log(this.state.phone);
  }
  logclick() {
    use_logo(this.state).then(res => {
      console.log(res);
      if (res.data.code == 200) {
        localStorage.setItem("token", "Bearer " + res.data.token)
        localStorage.setItem("userid", res.data.userinfo.id)
        this.props.history.push("/home")
      }

    })
    console.log(this.state);
  }
  render() {
    return (
      <div className='log_title'>
        <h3>用户登录</h3>
        <div className='log_name'><span>用户名:</span><input type="text" name="phone" defaultValue={this.state.phone}
          onChange={this.handenlclick("phone")} /></div>
        <div className='log_pass'><span>密码:</span><input type="pass" name="pass" defaultValue={this.state.pass}
          onChange={this.handenlclick("pass")} /></div>
        <div className='titlesas'> <Link to={"/register"}> 没有账号?去注册</Link></div>
        <button onClick={() => {
          this.logclick()
        }}>登录</button>
      </div>
    );
  }
}

export default withRouter(logo);